package com.chenyun.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenyun.system.mapper.SysMerchantMenuMapper;
import com.chenyun.system.model.SysMenu;
import com.chenyun.system.model.SysMerchantMenu;
import com.chenyun.system.service.ISysMenuService;
import com.chenyun.system.service.ISysMerchantMenuService;
import com.chenyun.system.vo.TreeSelect;
import com.chenyun.common.exceptions.BusinessException;
import com.chenyun.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分配商户菜单
 *
 * @author: Lonni
 * @date: 2023/3/30 0030 14:49
 */
@Service
@Slf4j
public class SysMerchantMenuServiceImpl extends ServiceImpl<SysMerchantMenuMapper, SysMerchantMenu> implements ISysMerchantMenuService {

    @Autowired
    private ISysMenuService menuService;


    /**
     * 更具商户类型获取菜单
     *
     * @param type
     * @return
     */
    @Override
    public List<SysMerchantMenu> getMerchantMenuByType(Integer type) {
        if (StringUtils.isNull(type)) {
            throw new BusinessException("商户类型必填!");
        }
        LambdaQueryWrapper<SysMerchantMenu> wrapper = Wrappers.lambdaQuery(SysMerchantMenu.class)
                .eq(SysMerchantMenu::getMerType, type);
        List<SysMerchantMenu> list = this.list(wrapper);
        return list;
    }

    @Override
    public List<TreeSelect> getMerchantMenuTreeSelectByType(Integer type) {
//        List<SysMerchantMenu> list = this.getMerchantMenuByType(type);
        //拿到菜单
        List<SysMenu> sysMenus = this.getMenuByType(type);
        List<TreeSelect> treeSelects = menuService.buildMenuTreeSelect(sysMenus);
        return treeSelects;
    }

    /**
     * 获取菜单
     *
     * @param type
     * @return
     */
    @Override
    public List<SysMenu> getMenuByType(Integer type) {
        List<SysMerchantMenu> list = this.getMerchantMenuByType(type);
        //拿到菜单
        List<SysMenu> sysMenus = menuService.selectMenu(list.stream().map(p -> p.getMenuId()).collect(Collectors.toSet()));
        return sysMenus;
    }

    /**
     * 分配菜单
     *
     * @param menus
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addMerchantMenu(List<SysMerchantMenu> menus) {

        if (StringUtils.isNull(menus.get(0).getMerType())) {
            throw new BusinessException("商户类型必填!");
        }
        List<SysMerchantMenu> merchantMenuByType = this.getMerchantMenuByType(menus.get(0).getMerType());
        List<Long> idList = merchantMenuByType.stream().map(SysMerchantMenu::getId).collect(Collectors.toList());
        this.removeBatchByIds(idList);
        this.saveBatch(menus);
        return true;
    }
}



